package control;

 
import java.awt.*;
import java.awt.event.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.*;
import model.Account;
import model.Share;
import model.Time;

public class TimerController extends javax.swing.JPanel{
    
    final static int interval = 1000;
    final static int jpbInterval = 100;
    private Calendar calendar = Calendar.getInstance();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");
    private Account account;
    private Share share;
    private Time time;
    private int i;
    private JLabel jlTimer;
    private JLabel jlClockTimer;
    private JProgressBar jpbTimerPeriod;
    private Timer timer;
    private JButton jbStart;
 
    
    public TimerController( Account controlledAccount, Share controlledShare, Time controlledTime ) {
        setAccount(controlledAccount);
        setShare(controlledShare);
        setTime(controlledTime);
        initComponents();
         
    }
    
    public void initComponents() {
        
        jbStart = new JButton("Start");
        jbStart.addActionListener(new ButtonListener());

        jpbTimerPeriod = new JProgressBar(0, jpbInterval);
        jpbTimerPeriod.setValue(0);
        jpbTimerPeriod.setStringPainted(true);

        jlTimer = new JLabel("Timer:");
        jlClockTimer = new JLabel("00:00");
        
        
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(jbStart, BorderLayout.PAGE_START);
        panel.add(jpbTimerPeriod, BorderLayout.SOUTH);
        panel.add(jlTimer, BorderLayout.PAGE_END);
        panel.add(jlClockTimer, BorderLayout.LINE_START );
        
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
         
         
        //Create a timer.
        timer = new Timer(interval, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if (i == jpbInterval){
                  Toolkit.getDefaultToolkit().beep();
                  timer.stop();
                  jbStart.setEnabled(true);
                  jpbTimerPeriod.setValue(0);
                  String str = "<html>" + "<font color=\"#FF0000\">" + "<b>" + 
                        "Period completed!" + "</b>" + "</font>" + "</html>";
                  jlTimer.setText(str);
                }
                i++;
                jpbTimerPeriod.setValue(i);
                calendar.setTimeInMillis(System.currentTimeMillis());
                String strClock = dateFormat.format(calendar.getTime());
                jlClockTimer.setText(strClock);
                }
        });
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
    
    public Share getShare() {
        return share;
    }
    
    public void setShare(Share share) {
        this.share = share;
    }

    public Time getTime() {
        return time;
    }
    
    private void setTime(Time time) {
        this.time = time;
    }

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            jbStart.setEnabled(false);
            i = 0;
            String strTimer = "<html>" + "<font color=\"#008000\">" + "<b>" + 
                        "Time is money....." + "</b>" + "</font>" + "</html>";
            
            jlTimer.setText(strTimer);
            timer.start();
        }
    }
     
}